﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using THEDARKKNIGHT.BatCore;
using THEDARKKNIGHT.EventSystem;
using UnityEngine.Video;
using System;
using THEDARKKNIGHT.ConstDefine;
using RenderHeads.Media.AVProVideo;

public class MediaPlayerCtrl : BatSingletion<MediaPlayerCtrl>{

    private string VideoName;

    private UpdateStereoMaterial Material;

    private MediaPlayer Player;

    private MediaPlayerCtrl() {
        GameObject RePlayer = Resources.Load<GameObject>("Prefabs/360SphereVideo");
        GameObject PlayerGB = GameObject.Instantiate<GameObject>(RePlayer);
        Material = PlayerGB.transform.Find("PlayArea").GetComponent<UpdateStereoMaterial>();
        Player = PlayerGB.transform.Find("MediaPlayer").GetComponent<MediaPlayer>();
        Material._camera = Camera.main;
        AddListener();
    }

    private void AddListener()
    {
        Player.Events.AddListener(MediaPlayerEventCallback);
        BEventManager<object>.Instance().AddListener(UIEventDefine.SEEKVIDEOTIME,SeekVideoTimeCallback);
        BEventManager<object>.Instance().AddListener(UIEventDefine.STARTTOPLAY, StartToPlayCallback);
        BEventManager<object>.Instance().AddListener(UIEventDefine.PAUSETOPLAY, PauseToPlayCallback);
        BEventManager<object>.Instance().AddListener(UIEventDefine.VIDEOPLAYSLIDERVALUE, GetSliderValueCallback);
        BEventManager<object>.Instance().AddListener(UIEventDefine.VIDEOPLAYTIME, GetPlayerTimeCallback);
    }

    private void MediaPlayerEventCallback(MediaPlayer player, MediaPlayerEvent.EventType type, ErrorCode error)
    {
        Debug.Log("MediaPlayerEventCallback :"+ type);
        switch (type) {
            case MediaPlayerEvent.EventType.Started:
                PerpareCompletedCallback();
                break;
            case MediaPlayerEvent.EventType.FinishedPlaying:
                VideoPlayCompleteCallback();
                break;
            case MediaPlayerEvent.EventType.FirstFrameReady:
                break;
        }
    }

    public void RemoveListener() {
        Player.Events.RemoveAllListeners();
        BEventManager<object>.Instance().RemoveListener(UIEventDefine.SEEKVIDEOTIME, SeekVideoTimeCallback);
        BEventManager<object>.Instance().RemoveListener(UIEventDefine.STARTTOPLAY, StartToPlayCallback);
        BEventManager<object>.Instance().RemoveListener(UIEventDefine.PAUSETOPLAY, PauseToPlayCallback);
        BEventManager<object>.Instance().RemoveListener(UIEventDefine.VIDEOPLAYSLIDERVALUE, GetSliderValueCallback);
        BEventManager<object>.Instance().RemoveListener(UIEventDefine.VIDEOPLAYTIME, GetPlayerTimeCallback);
    }

    private object GetPlayerTimeCallback(object data)
    {
        return (int)(Player.Control.GetCurrentTimeMs()/1000);
    }

    private object GetSliderValueCallback(object data)
    {
        return Player.Control.GetCurrentTimeMs()/ Player.Info.GetDurationMs();
    }


    private object PauseToPlayCallback(object data)
    {
        if (data != null) {
            VideoStatus status = (VideoStatus)data;
            if(!status.IsPlay && Player.Control.IsPlaying())
                 Player.Pause();
        }
        return null;
    }

    private object StartToPlayCallback(object data)
    {
        //TODO 开始播放以及恢复播放逻辑
        if (data != null) {
            VideoStatus status = (VideoStatus)data;
            if (status.VideoName != null)
            {
                //播放新的视频
                Player.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder,BFameWorkPathDefine.BVideoPath + "/" + status.VideoName + ".mp4");
                Player.Play();
            }
            else {
                //恢复暂定播放的视频
                if(status.IsPlay)
                    Player.Play();
                return false;
            }
        }
        return true;
    }

    private object SeekVideoTimeCallback(object data)
    {
        Player.Control.Seek((float)data * Player.Info.GetDurationMs());
        VideoStatus  videoData = new VideoStatus();
        videoData.CurrentTime = (int)(Player.Control.GetCurrentTimeMs()/1000);
        videoData.SumPlayTime = (int)(Player.Info.GetDurationMs() / 1000);
        videoData.VideoName = VideoName;
        BEventManager<object>.Instance().DispatchEvent(UIEventDefine.VIDEOTIMECHANGE, videoData);
        return null;
    }

    private void VideoPlayCompleteCallback()
    {
       Player.Stop();
       BEventManager<object>.Instance().DispatchEvent(UIEventDefine.VIDEOPLAYFINISH, null);
    }

    private void ClockResyncOccurredCallback(VideoPlayer source, double seconds)
    {
        Debug.Log("ClockResyncOccurredCallback :"+ seconds);
    }

    private void PerpareCompletedCallback()
    {
        VideoStatus videoData = new VideoStatus();
        videoData.CurrentTime = 0;
        videoData.SumPlayTime = (int)(Player.Info.GetDurationMs()/1000);
        Debug.Log(videoData.SumPlayTime);
        videoData.VideoName = VideoName;
        BEventManager<object>.Instance().DispatchEvent(UIEventDefine.READYTOPLAY, videoData);
        Debug.Log("PerPareCompletedCallback :");
    }
}
